package fy.bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import fy.util.DBUtil;;

public class SfgkBean {
	private int id;
	private String category;
	private String categoryname;
	private String title;
	private String content;
	private int homepage;
	private String author;
	private Date time;
	private int link;
	private int video;
	private String source;
	private int puton;

	public int getPuton() {
		return puton;
	}

	public void setPuton(int puton) {
		this.puton = puton;
	}

	PreparedStatement statement = null;
	ResultSet rs = null;
	Connection connection = null;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getTitle() {
		return title;
	}

	public String getCategoryname() {
		return categoryname;
	}

	public void setCategoryname(String categoryname) {
		this.categoryname = categoryname;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public int getHomepage() {
		return homepage;
	}

	public void setHomepage(int homepage) {
		this.homepage = homepage;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	public int getLink() {
		return link;
	}

	public void setLink(int link) {
		this.link = link;
	}

	public int getVideo() {
		return video;
	}

	public void setVideo(int video) {
		this.video = video;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	//通过时间排序
	public List<SfgkBean> findAllByLbForPX(String lbString) {
		System.out.println("run findAllByLb " + lbString);
		List<SfgkBean> list = new ArrayList<SfgkBean>();
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		String sql = "select * from sfgk where category = ? and puton=1 order by time desc";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, lbString);
			rs = statement.executeQuery();
			while (rs.next()) {
				SfgkBean sfgk = new SfgkBean();
				sfgk.setId(rs.getInt("id"));
				sfgk.setTitle(rs.getString("title"));
				sfgk.setContent(rs.getString("content"));
				sfgk.setTime(rs.getDate("time"));
				sfgk.setPicture(rs.getString("picture"));
				sfgk.setAuthor(rs.getString("author"));
				sfgk.setCategory(rs.getString("category"));
				list.add(sfgk);
				System.out.println("findAllByLb  " + sfgk);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return list;

	}

	// 查找上一页的ID
	public SfgkBean findPreNewsById(List<SfgkBean> listSfgkBean, int id) {
		SfgkBean sfgk = null;
		for (int i = 0; i < listSfgkBean.size(); i++) {
			if (id == listSfgkBean.get(i).getId() && i != 0) {
				sfgk = listSfgkBean.get(i - 1);
				return sfgk;
			}
		}
		return sfgk;

	}

	public SfgkBean findNextNewsById(List<SfgkBean> listSfgkBean, int id) {
		SfgkBean sfgk = null;
		for (int i = 0; i < listSfgkBean.size(); i++) {
			if (id == listSfgkBean.get(i).getId() && i + 1 < listSfgkBean.size()) {
				sfgk = listSfgkBean.get(i + 1);
				return sfgk;
			}
		}
		return sfgk;

	}

	// 放在首页的方法
	public List<SfgkBean> findOnindex() {
		List<SfgkBean> list = new ArrayList<SfgkBean>();
		DBUtil dbUtil = new DBUtil();
		String sql = "select * from sfgk where homepage=1 and puton=1 order by time desc";
		connection = dbUtil.geConnection();
		try {
			statement = connection.prepareStatement(sql);
			rs = statement.executeQuery();
			while (rs.next()) {
				SfgkBean sfgkBean = new SfgkBean();
				sfgkBean.setId(rs.getInt("id"));
				sfgkBean.setTitle(rs.getString("title"));
				sfgkBean.setContent(rs.getString("content"));
				sfgkBean.setAuthor(rs.getString("author"));
				sfgkBean.setSource(rs.getString("source"));
				sfgkBean.setTime(rs.getDate("time"));
				sfgkBean.setPicture(rs.getString("picture"));
				list.add(sfgkBean);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return list;

	}

	public SfgkBean findOnindexbyId(int id) {
		SfgkBean sfgkBean = new SfgkBean();
		DBUtil dbUtil = new DBUtil();
		String sql = "select * from sfgk where id=? and homepage=1 and puton=1";
		connection = dbUtil.geConnection();
		try {
			statement = connection.prepareStatement(sql);
			statement.setInt(1, id);
			rs = statement.executeQuery();
			while (rs.next()) {
				sfgkBean.setId(rs.getInt("id"));
				sfgkBean.setTitle(rs.getString("title"));
				sfgkBean.setContent(rs.getString("content"));
				sfgkBean.setAuthor(rs.getString("author"));
				sfgkBean.setSource(rs.getString("source"));
				sfgkBean.setTime(rs.getDate("time"));
				sfgkBean.setPicture(rs.getString("picture"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return sfgkBean;

	}

	public List<SfgkBean> findOnindexbylb(String lbString) {
		List<SfgkBean> list = new ArrayList<SfgkBean>();
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		String sql = "select * from sfgk where category =? and homepage=1 and puton=1 order by time desc ";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, lbString);
			rs = statement.executeQuery();
			while (rs.next()) {
				SfgkBean sfgkBean = new SfgkBean();
				sfgkBean.setId(rs.getInt("id"));
				sfgkBean.setTitle(rs.getString("title"));
				sfgkBean.setContent(rs.getString("content"));
				sfgkBean.setTime(rs.getDate("time"));
				sfgkBean.setAuthor(rs.getString("author"));
				sfgkBean.setSource(rs.getString("source"));
				sfgkBean.setCategory(rs.getString("category"));
				sfgkBean.setCategoryname(rs.getString("categoryname"));

				list.add(sfgkBean);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return list;

	}

	// .查询全部内容
	public List<SfgkBean> findAll() {
		List<SfgkBean> list = new ArrayList<SfgkBean>();
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		String sql = "select * from sfgk";
		try {
			statement = connection.prepareStatement(sql);
			rs = statement.executeQuery(sql);
			while (rs.next()) {
				SfgkBean sfgkBean = new SfgkBean();

				sfgkBean.setTitle(rs.getString("title"));
				sfgkBean.setContent(rs.getString("content"));
				sfgkBean.setHomepage(rs.getInt("homepage"));
				sfgkBean.setCategory(rs.getString("category"));
				sfgkBean.setCategoryname(rs.getString("categoryname"));
				sfgkBean.setAuthor(rs.getString("author"));
				sfgkBean.setTime(rs.getDate("time"));
				sfgkBean.setLink(rs.getInt("link"));
				sfgkBean.setVideo(rs.getInt("video"));
				sfgkBean.setSource(rs.getString("source"));

				list.add(sfgkBean);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return list;
	}

	// 增加内容
	public boolean doAdd(String category,String categoryname, String title, String source, String content, String author, String txtDate,
			int homepage, int puton) {
		boolean add = false;
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		// String sql = "select password from admin where username = " + username + "'";
		String sql = "insert into sfgk(category,categoryname,title,source,content,author,time,homepage,puton) values(?,?,?,?,?,?,?,?,?)";

		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, category);
			statement.setString(2, categoryname);
			statement.setString(3, title);
			statement.setString(4, source);
			statement.setString(5, content);
			statement.setString(6, author);
			statement.setString(7, txtDate);
			statement.setInt(8, homepage);
			statement.setInt(9, puton);
			System.out.println("结果前" + rs);
			System.out.println(add);
			int i = statement.executeUpdate();
			if (i > 0) {
				add = true;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return add;

	}

	// 删除内容
	public boolean doDel(int id) {
		boolean del = false;
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		String sql = "delete from sfgk  where id =?";
		// String sql = "insert into sfgk(v1,v2) values(?,getdate());";
		// String sql = "insert into sfgk(category,title,source,content,author,id)
		// values(?,?,?,?,?,?)";

		try {
			PreparedStatement statement = connection.prepareStatement(sql);

			statement.setInt(1, id);
			int i = statement.executeUpdate();
			if (i > 0) {
				del = true;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return del;

	}

	// 修改内容
	public boolean Updata(String category,String categoryname, String title, String source, String content, String author, String txtDate,
			int homepage, int puton, int id) {
		System.out.print("1" + category+categoryname + title + content + author + source + time + puton + id);
		boolean updata = false;
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		String sql = "update sfgk set category=?,categoryname=?,title=?,source=?,content=?,author=?,time=?,homepage=?,puton=? where id=?";

		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, category);
			statement.setString(2, categoryname);
			statement.setString(3, title);
			statement.setString(4, source);
			statement.setString(5, content);
			statement.setString(6, author);
			statement.setString(7, txtDate);
			statement.setInt(8, homepage);
			statement.setInt(9, puton);
			statement.setInt(10, id);
			System.out.println(updata);
			int i = statement.executeUpdate();
			if (i > 0) {
				updata = true;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return updata;

	}

//查询后总页数
	public int findCountByPro(String tilString) {
		List<SfgkBean> list = new ArrayList<SfgkBean>();
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		int count = 0;
		String sql = "select count(*) totalCount from sfgk where concat(id,title,content,category,categoryname,author,time,picture) like ?";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, "%" + tilString + "%");
			rs = statement.executeQuery();
			if (rs.next()) {
				count = rs.getInt("totalCount");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return count;

	}

	// 查询功能

	public List<SfgkBean> Proselect(String lbString, String tilString, Page page) {
		List<SfgkBean> list = new ArrayList<SfgkBean>();
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		// String sql = "select * from sfgk";
		String sql = "select * from sfgk where category =? and concat(category,categoryname,title,source,content,author,id) like ? order by time desc limit ?,?";
		connection = dbUtil.geConnection();
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, lbString);
			statement.setString(2, "%" + tilString + "%");
			statement.setInt(3, page.getBeginIndex());
			statement.setInt(4, page.getEveryPage());
			rs = statement.executeQuery();
			while (rs.next()) {
				SfgkBean SfgkBean = new SfgkBean();
				SfgkBean.setCategory(rs.getString("category"));
				SfgkBean.setCategoryname(rs.getString("categoryname"));
				SfgkBean.setTitle(rs.getString("title"));
				SfgkBean.setSource(rs.getString("source"));
				SfgkBean.setContent(rs.getString("content"));
				SfgkBean.setAuthor(rs.getString("author"));
				SfgkBean.setId(rs.getInt("id"));
				SfgkBean.setTime(rs.getDate("time"));
				list.add(SfgkBean);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return list;

	}

	// 后端查询总数
	public int findbackCountByLb(String lbString) {
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		int count = 0;
		String sql = "select count(*) totalCount from sfgk where category =? ";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, lbString);
			rs = statement.executeQuery();
			if (rs.next()) {
				count = rs.getInt("totalCount");
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return count;

	}

	// 后端分页
	public List<SfgkBean> findbackByLbForFY(String lbString, Page page) {
		List<SfgkBean> list = new ArrayList<SfgkBean>();
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		String sql = "select * from sfgk where category =? order by time desc limit ?,? ";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, lbString);
			statement.setInt(2, page.getBeginIndex());
			statement.setInt(3, page.getEveryPage());
			rs = statement.executeQuery();
			while (rs.next()) {
				SfgkBean sfgkBean = new SfgkBean();
				sfgkBean.setCategory(rs.getString("category"));
				sfgkBean.setCategoryname(rs.getString("categoryname"));
				sfgkBean.setHomepage(rs.getInt("homepage"));
				sfgkBean.setPuton(rs.getInt("puton"));
				sfgkBean.setId(rs.getInt("id"));
				sfgkBean.setTitle(rs.getString("title"));
				sfgkBean.setContent(rs.getString("content"));
				sfgkBean.setAuthor(rs.getString("author"));
				sfgkBean.setSource(rs.getString("source"));
				sfgkBean.setTime(rs.getDate("time"));
				sfgkBean.setPicture(rs.getString("picture"));
				list.add(sfgkBean);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return list;

	}

	// 后端模糊查询后总数
	public int findselectCountByLb(String lbString, String tilString) {
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		int count = 0;
		String sql = "select count(*) totalCount from sfgk where category =? and concat(id,title,content,category,categoryname,author,source,time,puton) like ? ";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, lbString);
			statement.setString(2, "%" + tilString + "%");
			rs = statement.executeQuery();
			if (rs.next()) {
				count = rs.getInt("totalCount");
				System.out.print("totalCount :" + count);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return count;

	}

	public SfgkBean findActiceById(String lb, int id) {
		System.out.println("分类：" + lb);
		System.out.println("id：" + id);
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		SfgkBean Sfgk = new SfgkBean();
		String sql = "select * from Sfgk where category = ? and id = ?";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, lb);
			statement.setInt(2, id);
			// String sql = "select * from 公告栏 where id = "+id;

			System.out.println(sql);
			rs = statement.executeQuery();

			if (rs.next()) {

				Sfgk.setId(rs.getInt("id"));
				Sfgk.setContent(rs.getString("content"));
				Sfgk.setCategory(rs.getString("category"));
				Sfgk.setAuthor(rs.getString("author"));
				Sfgk.setTime(rs.getDate("time"));

			}

		} catch (SQLException e) {
			// WhzbBean=null;
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return Sfgk;
	}

	public SfgkBean findOneByLb(String lbString) {
		SfgkBean newBean = new SfgkBean();
		DBUtil dbUtil = new DBUtil();
		String sql = "select * from sfgk where category = ? ";
		connection = dbUtil.geConnection();
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, lbString);
			rs = statement.executeQuery();
			if (rs.next()) {
				newBean.setId(rs.getInt("id"));
				newBean.setTitle(rs.getString("title"));
				newBean.setContent(rs.getString("content"));
				newBean.setPicture(rs.getString("picture"));
				newBean.setSource(rs.getString("source"));
				newBean.setAuthor(rs.getString("author"));
				newBean.setTime(rs.getDate("time"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return newBean;

	}

	private void setPicture(String string) {
		// TODO Auto-generated method stub

	}

//通过ID查找
	public SfgkBean findNewsById(int id) {
		System.out.println("run findNewsById : " + id);
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		SfgkBean sfgkBean = new SfgkBean();
		String sql = "select * from sfgk where id = ? ";
		try {
			statement = connection.prepareStatement(sql);
			statement.setInt(1, id);
			rs = statement.executeQuery();
			if (rs.next()) {
				sfgkBean.setId(rs.getInt("id"));
				sfgkBean.setTitle(rs.getString("title"));
				sfgkBean.setContent(rs.getString("content"));
				sfgkBean.setTime(rs.getDate("time"));
				sfgkBean.setHomepage(rs.getInt("homepage"));
				sfgkBean.setCategory(rs.getString("category"));
				sfgkBean.setCategoryname(rs.getString("categoryname"));
				sfgkBean.setAuthor(rs.getString("author"));
				sfgkBean.setTime(rs.getDate("time"));
				sfgkBean.setLink(rs.getInt("link"));
				sfgkBean.setVideo(rs.getInt("video"));
				sfgkBean.setSource(rs.getString("source"));
				sfgkBean.setPuton(rs.getInt("puton"));

			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		System.out.println("end findNewsById :");
		return sfgkBean;

	}

//通过类别查找

	public List<SfgkBean> findsfgkByLb(String lbString) {
		List<SfgkBean> list = new ArrayList<SfgkBean>();
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		String sql = "select * from sfgk where category =? ";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, lbString);
			rs = statement.executeQuery();
			while (rs.next()) {
				SfgkBean sfgkBean = new SfgkBean();
				sfgkBean.setId(rs.getInt("id"));
				sfgkBean.setTitle(rs.getString("title"));
				sfgkBean.setContent(rs.getString("content"));
				sfgkBean.setTime(rs.getDate("time"));
				sfgkBean.setAuthor(rs.getString("author"));
				sfgkBean.setSource(rs.getString("source"));
				sfgkBean.setCategory(rs.getString("category"));
				sfgkBean.setCategoryname(rs.getString("categoryname"));

				list.add(sfgkBean);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return list;

	}

//前台通过类别查找
	public List<SfgkBean> findQTByLb(String lbString) {
		List<SfgkBean> list = new ArrayList<SfgkBean>();
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		String sql = "select * from sfgk where category =? and puton=1";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, lbString);
			rs = statement.executeQuery();
			while (rs.next()) {
				SfgkBean sfgkBean = new SfgkBean();
				sfgkBean.setId(rs.getInt("id"));
				sfgkBean.setTitle(rs.getString("title"));
				sfgkBean.setContent(rs.getString("content"));
				sfgkBean.setTime(rs.getDate("time"));
				sfgkBean.setAuthor(rs.getString("author"));
				sfgkBean.setSource(rs.getString("source"));
				sfgkBean.setCategory(rs.getString("category"));
				sfgkBean.setPuton(rs.getInt("puton"));
				list.add(sfgkBean);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return list;

	}

//通过页数的类别查找
	public int findAllByLb(String lbString) {
		List<SfgkBean> list = new ArrayList<SfgkBean>();
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		int count = 0;
		String sql = "select count(*) totalCount from sfgk where category =? and puton=1";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, lbString);
			rs = statement.executeQuery();
			if (rs.next()) {
				count = rs.getInt("totalCount");
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return count;

	}

	// 页数
	public List<SfgkBean> findAllByLbForFY(String lbString, Page page) {
		List<SfgkBean> list = new ArrayList<SfgkBean>();
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		String sql = "select * from sfgk where category =? and puton=1 order by time desc limit ?,?";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, lbString);
			statement.setInt(2, page.getBeginIndex());
			statement.setInt(3, page.getEveryPage());
			rs = statement.executeQuery();
			while (rs.next()) {
				SfgkBean sfgkBean = new SfgkBean();
				sfgkBean.setCategory(rs.getString("category"));
				sfgkBean.setCategoryname(rs.getString("categoryname"));
				sfgkBean.setId(rs.getInt("id"));
				sfgkBean.setTitle(rs.getString("title"));
				sfgkBean.setContent(rs.getString("content"));
				sfgkBean.setAuthor(rs.getString("author"));
				sfgkBean.setSource(rs.getString("source"));
				sfgkBean.setTime(rs.getDate("time"));
				sfgkBean.setPicture(rs.getString("picture"));
				list.add(sfgkBean);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return list;

	}

	@Override
	public String toString() {
		return "SfgkBean [id=" + id + ", category=" + category + ", title=" + title + ", content=" + content
				+ ", homepage=" + homepage + ", author=" + author + ", time=" + time + ", link=" + link + ", video="
				+ video + ",source=" + source + "]";
	}

	public static void main(String[] args) {

	}

}
